﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using VideoSeller.Business.Data;
using VideoSeller.Business.Data.Enums;
using VideoSeller.Business.Models;
using VideoSeller.Core.Configuration;

namespace VideoSeller.Business.Extensions
{
	public static class ResourceExtension
	{
		public static string GetGlobalResource(this HtmlHelper helper, string classKey, string resourceKey)
		{
			var globalObject = HttpContext.GetGlobalResourceObject(classKey, resourceKey, CultureInfo.CurrentUICulture);
			return (globalObject != null && globalObject.ToString() != "") ? globalObject.ToString() : resourceKey;
		}

		public static string GetLocalResource(this HtmlHelper helper, string resourceKey)
		{
			var viewPath = ((RazorView) helper.ViewContext.View).ViewPath;
			var path = viewPath.Replace("~/", string.Empty).Replace("/", "-");
			var localObject = HttpContext.GetLocalResourceObject(path, resourceKey, CultureInfo.CurrentUICulture);
			return localObject != null && !string.IsNullOrEmpty(localObject.ToString()) ? localObject.ToString() : resourceKey;
		}

		public static IEnumerable<CultureResource> GetResources(this HtmlHelper helper, string resourceObject)
		{
			var resourceModel = IoC.Resolve<ResourceModel>();
			return resourceModel.GetResources(resourceObject);
		}

		public static string GetFullCultureName(this HtmlHelper helper, string shortCultureName)
		{
			var culture = new CultureInfo(shortCultureName);
			return culture.NativeName;
		}
        public static string GetCurrencyString( decimal many)
        {
            var culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            return "$" + many.ToString("0.00").Replace(",", ".").Replace(".00", "");
            // return string.Format(culture, "{0:C}", many);
        }
        public static string GetCurrencyString(this  HtmlHelper helper, decimal many)
        {
            return GetCurrencyString(many);
            //var culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            //return "$"+many.ToString("0.00").Replace(",",".").Replace(".00","");
           // return string.Format(culture, "{0:C}", many);
        }

		public static string GetThumbnailImage(this UrlHelper helper, string url, ThumbnailImageSize imageSize)
		{
			var parts = url.Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries);

			if (parts.Length == 2)
			{
				return helper.Content(string.Format("{0}_{1}.{2}", parts[0], imageSize, parts[1]));
			}

			return helper.Content(url);
		}
	}
}